﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine.Scenes
{
    public class ActorManager
    {
        #region Attributes
        private List<Actor> actors = new List<Actor>();
        private List<Actor> actorsToAdd = new List<Actor>(), actorsToRemove = new List<Actor>();
        private bool bloqued = false;
        #endregion

        #region Functionality
        public void update(int dTime)
        {
            bloqued = true;
            foreach (Actor actor in actors)
            {
                actor.update(dTime);
            }
            bloqued = false;
            actorsLazyAddRem();
        }
        public void draw(SpriteBatch sb)
        {
            bloqued = true;
            foreach (Actor actor in actors)
            {
                actor.draw(sb);
            }
            bloqued = false;
            actorsLazyAddRem();
        }
        public List<Actor> getActors()
        {
            return actors;
        }
        public void actorsLazyAddRem()
        {
            if (actorsToAdd.Count > 0)
            {
                actors.AddRange(actorsToAdd);
                actorsToAdd.Clear();
            }
            if (actorsToRemove.Count > 0)
            {
                foreach (Actor rem in actorsToRemove)
                    actors.Remove(rem);
                actorsToRemove.Clear();
            }
        }
        public void addActor(Actor newActor) 
        {
            if (bloqued)
                actorsToAdd.Add(newActor);
            else
                actors.Add(newActor);
        }
        public void addActors(List<Actor> newActors) 
        {
            if (bloqued)
                actorsToAdd.AddRange(newActors);
            else
                actors.AddRange(newActors);
        }
        public void removeActor(Actor delActor) 
        {
            if (bloqued)
                actorsToRemove.Add(delActor);
            else
                actors.Remove(delActor);
        }
        public void removeActors(List<Actor> delActors) 
        {
            if (bloqued)
                actorsToRemove.AddRange(delActors);
            else
                foreach (Actor del in delActors)
                    actors.Remove(del);
        }
        private static bool isSameActor(String s)
        {
            return s.ToLower().EndsWith("saurus");
        }
        #endregion
    }
}
